<template>
  <div class="default-module" :style="isSingle ? '' : moduleStyle">
    <template v-for="comp in components" :key="comp.key">
      <div
        class="grid-item"
        :class="{ 'one-module': isSingle }"
        :style="
          isSingle
            ? 'height: 100%'
            : moduleStyle && moduleStyle['grid-template-areas']
            ? { 'grid-area': comp.key }
            : {}
        "
      >
        <component :ref="comp.key" :is="'Rev' + comp.type" :config="comp" :singlePage="isSingle" />
      </div>
    </template>
  </div>
</template>

<script>
export default {
  name: 'Default',
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.module = to.meta.config
    })
  },
  props: {
    config: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      module: null,
      delayStyle: null
    }
  },
  computed: {
    configMudule() {
      return this.module || this.config
    },
    components() {
      return this.configMudule?.components || []
    },
    isSingle() {
      return this.components.length < 2
    },
    moduleStyle() {
      return {
        ...this.delayStyle,
        ...this.configMudule?.layout?.style
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      // 延迟设置样式，防止item大小不是100%
      this.delayStyle = {
        'align-content': 'start',
        'justify-content': 'start'
      }
    })
  }
}
</script>

<style scoped lang="less">
.default-module {
  display: grid;
  grid-template-columns: 100%;
  // justify-content: start;
  // align-content: start;
  width: 100%;
  height: 100%;
  min-height: calc(
    100vh - var(--tags-view-height) - var(--top-tool-height) - var(--app-footer-height) -
      var(--app-content-padding) - var(--app-content-padding) - 1px
  );
  grid-gap: 20px;
  place-content: unset;

  .grid-item {
    overflow: auto;
  }
}
</style>
